package com.sttk.secreconeclick.bookkeep.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.basic.dialog.BaseSmartDialog;
import com.lzb.basic.utils.SPUtil;
import com.sttk.secreconeclick.bookkeep.Content;
import com.sttk.secreconeclick.bookkeep.R;
import com.sttk.secreconeclick.bookkeep.adapter.SelAccountBookAdapter;
import com.sttk.secreconeclick.bookkeep.bean.AccountBookBean;
import com.sttk.secreconeclick.bookkeep.databinding.DialogChangeCycleBinding;
import com.sttk.secreconeclick.bookkeep.databinding.DialogSelAccountBookBinding;
import com.sttk.secreconeclick.bookkeep.utils.AppSpUtils;

import java.util.List;

public class SelAccountBookDialog extends BaseSmartDialog<DialogSelAccountBookBinding> implements View.OnClickListener {

    private SelAccountBookAdapter selAccountBookAdapter;
    private int selPosition;

    public SelAccountBookDialog(@NonNull Context context) {
        super(context);
        this.selPosition = -1;
    }

    public void setSelPosition(int selPosition) {
        this.selPosition = selPosition;
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_sel_account_book;
    }

    @Override
    protected void initView(View view) {

        mDataBinding.rvAccountBookList.setLayoutManager(new LinearLayoutManager(getContext()));
        selAccountBookAdapter = new SelAccountBookAdapter();
        mDataBinding.rvAccountBookList.setAdapter(selAccountBookAdapter);
        selAccountBookAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                selAccountBookAdapter.getItem(position).setSelected(true);
                selAccountBookAdapter.notifyItemChanged(position);
                dismiss();
                if (listener != null) {
                    listener.clickItem(position);
                }
            }
        });

        List<AccountBookBean> list = AppSpUtils.getAccountBookList();

        if (selPosition == -1) {
            mDataBinding.tvTitle.setText(getContext().getString(R.string.sel_account_book));
        } else {
            mDataBinding.tvTitle.setText(getContext().getString(R.string.import_from_other_account_book));
            list.remove(selPosition);
        }
        selAccountBookAdapter.setList(list);
        selAccountBookAdapter.setSelPosition(selPosition);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickItem(int pos);
    }
}
